{% extends "base.html" %}

{% block title %}
    {{ super() }}
    - 授权{% endblock %}

{% block body_content %}
    <div class="col-sm-offset-6 col-sm-6">
        <h2>授权</h2>
    </div>
    <form class="form-horizontal" method="post" action="{{ url_for('auth_view.authorize') }}">
        <div class="form-group">
            <label for="div_role" class="col-sm-2 control-label">角色</label>
            <div class="col-sm-10">
                <div id="div_role">
                    {% if roles %}
                        {% for role in roles %}
                            <div class="radio">
                                <input  type="radio" name="role" id="role_{{ role.id }}" value="{{ role.id }}" >{{ role.name }}
                            </div>
                        {% endfor %}
                    {% endif %}
                </div>
            </div>
        </div>

        <div class="form-group">
            <label for="div_role" class="col-sm-2 control-label">权限</label>
            <div class="col-sm-10">
                <div id="div_permission">

                    {% if permissions %}
                        {% for item in permissions %}
                            <div class="checkbox">
                                <input  type="checkbox" name="permission" id="permission_{{ item.id }}" value="{{ item.id }}" >{{ item.name }}
                            </div>
                        {% endfor %}
                    {% endif %}
                </div>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-6">
                <input type="submit"  value="保存"/>
            </div>
        </div>
    </form>

    <script type="text/javascript">
        var url_prefix = '/blog';
        $(function(){
            $(':input[name="role"]').on('click',function(){
                $(':input[name="permission"]').each(function(i,e){
                    if(e.checked){
                        e.removeAttribute('checked');
                    }
                });
                $.ajax({
                    type:'GET',
                    url:url_prefix+'/auth/permissions/'+$(this).val(),
                    success:function(ret){
                        data = eval('('+ret+')');
                        for(var i=0;i<data.length;i++){
                            $('#permission_'+data[i].permission).attr('checked','checked');
                        }
                    },
                    error:function(ret){
                        console.log('error:'+ret);
                    }
                });
            });
        });

    </script>
{% endblock %}